package com.thomasgustavo.meugood;


import java.util.ArrayList;
import java.util.List;

public class Saldo {

	private String pagina;
	
	boolean cartaoPremium;
	
	private List<Transacao> transacoes;

	public Saldo(String pagina, boolean cartaoPremium) {
		this.pagina = pagina;
		transacoes = new ArrayList<Transacao>();
		this.cartaoPremium = cartaoPremium;
		createTransacoes();
	}

	private void createTransacoes() {
		if( cartaoPremium ){
			for( String transacao : StringUtils.getConteudos(pagina, "<tr class", "</tr>")){
				if(Transacao.isTransacaoPremium(transacao)){
					transacoes.add(new Transacao(transacao, true));
				}
			}
		} else {
			for( String transacao : StringUtils.getConteudos(pagina, "<tr align=\"center\" valign=\"top\">", "</tr>")){
				transacoes.add(new Transacao(transacao, false));
			}
		}
	}

	@Override
	public String toString() {
		return pagina;
	}
	
	public String getValorSaldo() {
		if( cartaoPremium ){
			return getValorSaldoPremium();
		} else {
			return getValorSaldoPlus();
		}
	}

	private String getValorSaldoPlus() {
		String aux = pagina;
		aux = aux.substring(aux.indexOf("<td>Saldo:</td>"));
		return StringUtils.getConteudo(aux, "<td><b>", "</b></td>");
	}

	private String getValorSaldoPremium() {
		String aux = pagina;
		aux = aux.substring(aux.indexOf("<td class=\"eQuebraL\">Total</td>"));
		return StringUtils.getConteudo(aux, "<td class=\"eQuebraL\" align=\"right\">", "</td>");
	}
	
	public List<Transacao> getTransacoes() {
		return transacoes;
	}

	public boolean isCartaoPremium() {
		return cartaoPremium;
	}
}
